package XMLStuff;

import JET.Centers.ControlCenter;
import JET.Centers.Graphics.GUICenter;
import JET.mygui.CenterTabsPane;
import JET.mygui.MultiSplitPane;
import JET.mygui.MyGuiHelper;
import JET.mygui.UtilTabsPane;
import JET.mygui.UtilTabsPane.UtilTabsPaneTab;
import java.awt.Component;
import javax.swing.JComponent;
import org.w3c.dom.*;

/**
 *
 * @author raos
 */
public class XMLHelper {

    ////////////////////////////////////////////////////////////////////////////
    //                        G U I   S T U F F                               //
    ////////////////////////////////////////////////////////////////////////////

    static public MultiSplitPane readMultiSplitPane(Element mspElem, ControlCenter CC) {

        int     cells       = Integer.parseInt(mspElem.getAttribute("cells"));
        boolean horizontal  = Boolean.parseBoolean(mspElem.getAttribute("horizontal"));
        MultiSplitPane rootMSP = new MultiSplitPane(cells, horizontal);

//        System.out.println("_!_ XMLHelper.readMultiSplitPane(), cells: "+cells+", horiz: "+horizontal);

        int cellIndex = 0;

        NodeList children = mspElem.getChildNodes();
//        System.out.println("_!_ XMLHelper.readMultiSplitPane(), parsing child nodes, length: "+children.getLength());
        for (int a = 0; a < children.getLength(); a++) {
            Node node = children.item(a);
            
            if (node instanceof Element) {
                Element elem = (Element) node;
//                System.out.println("_!_ XMLHelper.readMultiSplitPane(), elem["+a+"]: "+elem);

                // Cell node
                if (elem.getTagName().equals("cell")) {
                    NodeList cellNodes = elem.getChildNodes();
//                    System.out.println("_!_ XMLHelper.readMultiSplitPane(), found 'cell', its children length: "+cellNodes.getLength());
                    for(int b=0; b<cellNodes.getLength(); b++) {
                        Node cellNode = cellNodes.item(b);

                        if (cellNode instanceof Element) {
                            Element cellElem = (Element) cellNode;

                            // MultiSplitPane node
                            if (cellElem.getTagName().equals("msp")) {
                                rootMSP.setCellComponent(cellIndex, readMultiSplitPane(cellElem, CC), false);
                            }
                            // CenterTabsPane node
                            else if(cellElem.getTagName().equals("centerTabsPane")) {
                                rootMSP.setCellComponent(cellIndex, readCenterTabsPane(cellElem, CC), false);
                            }
                            // UtilTabsPane node
                            else if(cellElem.getTagName().equals("utilTabsPane")) {
                                rootMSP.setCellComponent(cellIndex, readUtilTabsPane(cellElem, CC), false);
                            }
                        }
                    }
                    // Set cell size
                    int cellSize = Integer.parseInt(elem.getAttribute("size"));
                    rootMSP.setCellSize(cellIndex, cellSize);
                    cellIndex++;
                }

            }
        }

        return rootMSP;
    }

    static public UtilTabsPane readUtilTabsPane(Element utpXML, ControlCenter CC) {
        UtilTabsPane utp = new UtilTabsPane("");

        NodeList children = utpXML.getChildNodes();
        for (int a = 0; a < children.getLength(); a++) {
            Node node = children.item(a);
            if (node instanceof Element) {
                Element elem = (Element) node;

                // Cell node
                if (elem.getTagName().equals("tab")) {
                    String id = elem.getAttribute("id");
                    UtilTabsPaneTab tab = null;
                    if (id.equals(GUICenter.UtilTabs.Bodies.toString()))                    tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Bodies);
                    else if (id.equals(GUICenter.UtilTabs.Mass.toString()))                 tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Mass);
                    else if (id.equals(GUICenter.UtilTabs.Materials.toString()))            tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Materials);
                    else if (id.equals(GUICenter.UtilTabs.RSpring.toString()))              tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.RSpring);
                    else if (id.equals(GUICenter.UtilTabs.Spring.toString()))               tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Spring);
                    else if (id.equals(GUICenter.UtilTabs.SpringBody.toString()))           tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.SpringBody);
                    else if (id.equals(GUICenter.UtilTabs.Tools.toString()))                tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Tools);
                    else if (id.equals(GUICenter.UtilTabs.ToolsOptions.toString()))         tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.ToolsOptions);
                    else if (id.equals(GUICenter.UtilTabs.Torque.toString()))               tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Torque);
                    else if (id.equals(GUICenter.UtilTabs.Output.toString()))               tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Output);
                    else if (id.equals(GUICenter.UtilTabs.SceneInspector.toString()))       tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.SceneInspector);
                    else if (id.equals(GUICenter.UtilTabs.SceneItemsInspector.toString()))  tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.SceneItemsInspector);
                    else if (id.equals(GUICenter.UtilTabs.Project.toString()))              tab = CC.GUIC.getUtilTab(GUICenter.UtilTabs.Project);

                    if (tab.isScrollable())
                        utp.add( MyGuiHelper.getNewJScrollPane((Component)tab) );
                    else
                        utp.add( (JComponent)tab );
                }
            }
        }

        return utp;
    }

    static public CenterTabsPane readCenterTabsPane(Element ctpXML, ControlCenter CC) {
        CenterTabsPane ctp = new CenterTabsPane("");

        NodeList children = ctpXML.getChildNodes();
        for (int a = 0; a < children.getLength(); a++) {
            Node node = children.item(a);
            if (node instanceof Element) {

                Element elem = (Element) node;
                // Cell node
                if (elem.getTagName().equals("tab")) {
                    String id = elem.getAttribute("id");
                    if (id.equals(CenterTabsPane.TabType.GraphicPanel.toString())) {
                        //ctp.add( CC.GUIC.getGraphicPanelContainer());
                    }
                    else if (id.equals(CenterTabsPane.TabType.JavaScriptEditor)) {
//                        String title = elem.getAttribute("title");
//
//                        MyScript script = scene.SC.getScript(title);
//                        if (script != null) {
//                            CPScriptEditor editor = new CPScriptEditor();
//                            editor.setScript(script);
//                            CC.GUIC.registerScript(script, (CenterTabsPaneTab) editor);
//                            ctp.add(editor);
//                        }
                    }
                }
            }
        }
        
        return ctp;
    }

   
}